﻿Imports System.Windows.Forms
Imports MapWindow.Map
Imports MapWindow.Components
Imports MapWindow.PluginInterfaces

    <MapWindowPlugin("SamplePluginVB", Author:="MyName", UniqueName:="SamplePluginVB_1", Version:="1.0")> _
    Public Class Main
        Inherits Extension
        Implements IMapPlugin

#Region "Variables"

    'Use the _mapWin variable to access the map, legend, toolbar and menu items.
    Private _mapWin As IMapPluginArgs

    'Keep track of menu items and toolbar buttons 
    'added by the plug-in so that they can be removed
    'when the plug-in is deactivated
    Private _menuStack As New Stack(Of ToolStripMenuItem)
    Private _buttonStack As New Stack(Of ToolStripButton)

    'A sample form launched when the toolbar button is clicked
    Private frmInfo As InfoForm

#End Region

#Region "IMapPlugin Members"

    '/// <summary>
    '/// Initialize the MapWindow 6 plugin
    '/// </summary>
    '/// <param name="args">The plugin arguments to access the main application</param>
    Public Sub Initialize(ByVal args As IMapPluginArgs) Implements IMapPlugin.Initialize

        'Assign the _mapWin variable to access the map and other properties
        _mapWin = args

        'Add a new toolbar button
        Dim btnLaunch As New ToolStripButton
        btnLaunch.DisplayStyle = ToolStripItemDisplayStyle.Image
        btnLaunch.Image = My.Resources.Images.Image1
        btnLaunch.ToolTipText = "Click the button"
        _mapWin.MainToolStrip.Items.Add(btnLaunch)
        _buttonStack.Push(btnLaunch)
        AddHandler btnLaunch.Click, AddressOf btnLaunch_Click

        'Add a new menu item
        Dim mnuSample As New ToolStripMenuItem("Sample Plugin")
        Dim mnuData As New ToolStripMenuItem("Menu 1")
        Dim mnuQuery As New ToolStripMenuItem("Menu 2")
        mnuSample.DropDownItems.Add(mnuData)
        mnuSample.DropDownItems.Add(mnuQuery)
        _mapWin.MainToolStrip.Items.Add(mnuSample)
        _menuStack.Push(mnuSample)

        'handle menu item click events
        AddHandler mnuData.Click, AddressOf mnuData_Click
        AddHandler mnuQuery.Click, AddressOf mnuQuery_Click

    End Sub

    Public Overloads Sub Activate() Implements IMapPlugin.Activate
        MyBase.Activate()
    End Sub

    Public Overloads Sub Deactivate() Implements IMapPlugin.Deactivate
        MyBase.Deactivate()
    End Sub

    Public Overloads Property IsEnabled() As Boolean Implements IMapPlugin.IsEnabled
        Get
            Return MyBase.IsEnabled
        End Get
        Set(ByVal value As Boolean)
            MyBase.IsEnabled = value
        End Set
    End Property

#End Region

#Region "Extension Members"

    Protected Overrides Sub OnDeactivate()

        'Remove the menu items
        While _menuStack.Count > 0
            _mapWin.MainMenu.Items.Remove(_menuStack.Pop())
        End While
        
        'Remove the toolbar buttons
        While _buttonStack.Count > 0
            _mapWin.MainToolStrip.Items.Remove(_buttonStack.Pop())
        End While

    End Sub

#End Region

#Region "Event Handlers"
    'When the toolbar button is clicked, launch a form
    Sub btnLaunch_Click(ByVal sender As Object, ByVal e As EventArgs)
        'Show the form
        If frmInfo Is Nothing Then
            frmInfo = New InfoForm(_mapWin)
        End If
        If frmInfo.Visible = False Then
            frmInfo.Show()
            frmInfo.UpdateLayerInfo()
        End If
    End Sub

    'When the 'Add Data' menu item is clicked
    Sub mnuData_Click(ByVal sender As Object, ByVal e As EventArgs)
        MsgBox("You clicked the 'menu 1' menu item.")
    End Sub

    'When the 'Find Country' menu item is clicked
    Sub mnuQuery_Click(ByVal sender As Object, ByVal e As EventArgs)
        MsgBox("You clicked the 'menu 2' menu item.")
    End Sub

#End Region

    End Class

